{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "joint-exchange",
   "metadata": {},
   "source": [
    "# Mauritian National ID Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "removed-brazilian",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "interested-start",
   "metadata": {},
   "source": [
    "The function `clean_mu_nid()` cleans a column containing Mauritian national ID number (NID) strings, and standardizes them in a given format. The function `validate_mu_nid()` validates either a single NID strings, a column of NID strings or a DataFrame of NID strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "animated-physiology",
   "metadata": {},
   "source": [
    "NID strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"J2906201304089\"\n",
    "* `standard`: NID strings with proper whitespace in the proper places. Note that in the case of NID, the compact format is the same as the standard one.\n",
    "* `birthdate`: split the date parts from the number and return the birth date, like \"2020-06-29\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_mu_nid()` and `validate_mu_nid()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tired-threat",
   "metadata": {},
   "source": [
    "### An example dataset containing NID strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "racial-solution",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"nid\": [\n",
    "            'J2906201304089',\n",
    "            'J2906201304088',\n",
    "            '7542011030',\n",
    "            '7552A10004',\n",
    "            '8019010008', \n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blank-arrangement",
   "metadata": {},
   "source": [
    "## 1. Default `clean_mu_nid`\n",
    "\n",
    "By default, `clean_mu_nid` will clean nid strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "determined-closure",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_mu_nid\n",
    "clean_mu_nid(df, column = \"nid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "warming-morning",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "physical-master",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mechanical-wheat",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "designed-pulse",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, column = \"nid\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numerical-margin",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "instructional-consumer",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, column = \"nid\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "separate-proof",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dated-sleeve",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, column = \"nid\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "first-communications",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned NID strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cloudy-subscriber",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, column=\"nid\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "academic-yukon",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "equipped-confidence",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "stuck-multiple",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, \"nid\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "utility-million",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "registered-trunk",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_mu_nid(df, \"nid\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "animal-feeding",
   "metadata": {},
   "source": [
    "## 4. `validate_mu_nid()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "statewide-clerk",
   "metadata": {},
   "source": [
    "`validate_mu_nid()` returns `True` when the input is a valid NID. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_mu_nid()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_mu_nid()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_mu_nid()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sized-zimbabwe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_mu_nid\n",
    "print(validate_mu_nid('J2906201304089'))\n",
    "print(validate_mu_nid('J2906201304088'))\n",
    "print(validate_mu_nid('7542011030'))\n",
    "print(validate_mu_nid('7552A10004'))\n",
    "print(validate_mu_nid('8019010008'))\n",
    "print(validate_mu_nid(\"hello\"))\n",
    "print(validate_mu_nid(np.nan))\n",
    "print(validate_mu_nid(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blocked-precipitation",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "still-vampire",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_mu_nid(df[\"nid\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brown-antibody",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "younger-front",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_mu_nid(df, column=\"nid\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "serious-uncertainty",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "certain-services",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "validate_mu_nid(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "black-fundamentals",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
